<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>订单管理管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				rules:{          
					/* logistics:{required:true,stringCheck:true}, */
					logisticsCode:{required:true,logisticsCode:true}
					
	            },
	            messages:{
	            	cnName:{required:'请选择发货仓库'},
	            	/* logistics:{required:'请输入物流公司'}, */
	            	logisticsCode:{required:'请输入物流单号'}
	            },
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
			/* jQuery.validator.addMethod("stringCheck", function(value, element) {
				return this.optional(element) || /^[\u4E00-\u9FA5][\u4E00-\u9FA5|A-Z|a-z|0-9]{3,20}$/.test(value);
               
			}, "输入4-20个字符，以汉字开头，可带字母或数字"); */
			
			jQuery.validator.addMethod("logisticsCode", function(value, element) {
				return this.optional(element) || /^[a-zA-Z0-9]{5,20}$/.test(value);
			}, "5-20个数字或字母");
		});
		
		
		$(function(){
			$('#btnSubmit').click(function(){
				var submited=true;
				var i=0;
				$('input[name="fhNum"]').each(function(k,v){
					i++;
					num=parseInt($(v).val());
					max=parseInt($(v).attr('data-maxnum'));
					if(num > max){
						showTip('商品发货数量大于剩余数量!','',3000,0);
						submited=false;
						return false;
					}
					if(num <1){
						showTip('商品发货数量要大于0!','',3000,0);
						submited=false;
						return false;
					}
				})
				return submited;
			})
		})
		
		function sty(){
			var id=$("#cnName").val();
			var goodsCode="";
			$('input[name="goodsCode"]').each(function(k,v){
				if(k != 0){
					goodsCode+=','+$(v).val();
				}else{
					goodsCode+=$(v).val();
				}
				
			})
		//	return false;
			$.ajax({
				type:"post",
				url:"${ctx}/sys/orders/showGoods",
				dataType:"json",
				data:{"id":id,"goodsCode":goodsCode},
				success:function(data){
					$('.control-group').show();
					$('.control-group').find('input').removeAttr('disabled');
					$.each(data,function(k,v){
						$('input[name="goodsCode"]').each(function(ck,cv){
							if($(cv).val()== v){
								$(this).parents('.control-group').hide();
								$(this).parents('.control-group').find('input').attr('disabled','disabled');
							}
						})
					})
					
				}
			})
		}
		
		 $(function(){
			var key="35dbd972c9dd4a492b01c771a0859971";
			$.ajax({
		        type : "post",
		        url :"http://v.juhe.cn/exp/com?key="+key,
		        dataType:"jsonp",
		        success : function(data) {
		            //$('#logistics').append('<option selected="selected" value="">选择物流公司</option>');
		            var data=data.result;
		            console.log(data);
		            if(data!=null){
		            	$(data).each(function(k,v){
		            		var $option = $("<option>").attr({
	                            "value" : v.com
	                        }).text(v.com);
		            		$("#logistics").append($option);
		            	})
		            	 
		            }
		        }
		        
		    });
		   })
	
		   function stgg(){
			$("#inputForm").valid();
		}
	</script>
</head>
<body>
	<form:form id="inputForm" modelAttribute="ordersDetail"  method="post" class="form-horizontal" action="${ctx }/sys/orders/saveGoodsNum">
	    <input name="code" value="${code}" id="code" type="hidden">
		<sys:message content="${message}"/>	
		<div class="control-group">
			<label class="control-label">发货仓库：</label>
			<div class="controls">
				<form:select id="cnName" path="cnName" class="input-medium required" onclick="sty();" onchange="stgg()">
				<form:option value="" label="请选择仓库"/>
			    <form:options items="${storeWarehouseList}" htmlEscape="false" itemValue="id" itemLabel="cnName"/></form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
	   <c:forEach var="orderDetail1" items="${orderDetail1 }">
		<div class="control-group">
			<label class="control-label" style='float:left;'>商品名称：</label>
			<div class="controls"  style='float:left;margin-left:20px'>
				<form:input path="goodsCnName" value="${orderDetail1.goodsCnName}" htmlEscape="false" maxlength="30" class="input-xlarge " readonly="true"/>
				<input name="detailId" value="${orderDetail1.id }" type="hidden" id="detailId">
				<input name="goodsCode" value="${orderDetail1.goodsCode }" type="hidden" id="goodsCode">
			</div>
			<label class="control-label"  style='float:left;'>发货数量：</label>
			<div class="controls"  style='float:left;margin-left:20px'>
				<form:input path="fhNum" data-maxnum='${orderDetail1.yuNum }' value="${orderDetail1.yuNum }" htmlEscape="false" maxlength="30" class="input-xlarge required digits"/>
				<input name="yusNum" value="${orderDetail1.yuNum }" type="hidden"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		</c:forEach>
		<div class="control-group">
			<label class="control-label">物流公司:</label>
			<div class="controls">
				 <form:select id="logistics" path="logistics" class="input-medium required" onchange="stgg()">
			     <form:option value="" label="选择物流公司"/>
			     </form:select>
			      <span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		
		<div class="control-group">
			<label class="control-label">物流单号:</label>
			<div class="controls">
				<form:input path="logisticsCode" htmlEscape="false" maxlength="50" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		
		<div class="form-actions">
		    <shiro:hasPermission name="sys:orders:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
	
</body>
</html>